package com.saic.data.table;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;

import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hdfs.server.namenode.INodeReference.WithCount;

import com.saic.data.dotask.DeleteTmpRecord;
import com.saic.data.dotask.UpdataDB;
import com.saic.data.util.DBUtil;
import com.saic.data.util.FileUtil;
import com.saic.data.util.HbaseUtil;

public class TableFile {

	private String fileName;
	private String ftpFileName;
	private String financeDate;
	private String financeYear;
	private String financeMonth;
	private String streamNo;
	private String modifiedDate;
	private String rowKey= "";
	private TableConf tConf;
	private Map<String,String> fileCol;
	private TableAction tAction;
	
	public TableFile(String ftpFileName,TableConf tConf,String modifiedDate){
		setFtpFileName(ftpFileName);
		this.modifiedDate = modifiedDate;
		settConf(tConf);
		setFileName();
		setStreamNo();
		setFinanceDate();
		setFileCol(tConf);
	}
	
	public TableFile(Result result,Map<String, String> tMap) throws UnsupportedEncodingException{
		rowKey = new String(result.getRow(),"UTF-8");
		tAction = TableAction.fromString(HbaseUtil.getFromResult(result,TableUtil.configtabFamily,"fileStats"));
		modifiedDate = HbaseUtil.getFromResult(result,TableUtil.configtabFamily,"modifiedDate");
		setFileName(HbaseUtil.getFromResult(result,TableUtil.configtabFamily,"fileName"));
		String tablename = HbaseUtil.getFromResult(result,TableUtil.configCustomFamily,"tableName");
		settConf(new TableConf(tablename, tMap.get(tablename)));
		setFinanceDate(HbaseUtil.getFromResult(result,TableUtil.configCustomFamily,"financeDate"));
		setStreamNo(HbaseUtil.getFromResult(result,TableUtil.configCustomFamily,"streamNo"));
		setFileCol(gettConf());	
	}
	
	public void setFileCol(TableConf tConf) {
		Map<String,String> map = tConf.getColMap();
		map.put("UUID", "custom11");
		map.put("fileKey", "custom12");
		map.put("fileName", "custom13");
		map.put("upLoadDate", "custom14");
		map.put("streamNo", "custom15");
		setFileCol(map);
	}
	
	/**
	 * @获得插入ORACLE数据库的INSERT语句
	 * @param r
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String getInsertSQL(Result r) throws UnsupportedEncodingException  {
		StringBuffer first = new StringBuffer();
		StringBuffer second = new StringBuffer();
		Map<String, String> map = getFileCol();
		for (String key : map.keySet()) {			
			first.append(map.get(key) + ",");
			try {
				second.append("\'" + HbaseUtil.format(HbaseUtil.getFromResult(r,gettConf().dataFamily, key)) + "\',");
			} catch (NullPointerException e) {
				// TODO Auto-generated catch block
				System.out.println(key+":"+map.get(key));
				e.printStackTrace();
				return "";
			}
		}
		return "insert into " + gettConf().getOracleTbl() + "("
				+ first.substring(0, first.length() - 1).toString() + 
				") values (" + second.substring(0, second.length() - 1).toString() + ")";
	}
	
	public String getModifieddate() {
		return modifiedDate;
	}

	public void setStreamNo() {
		String streamNo = null;
		if (getFileName().length() < gettConf().getDendPoint())
			streamNo = "";
		if (getFileName().length() == gettConf().getDendPoint())
			streamNo = getFileName().substring(0, gettConf().getDstartPoint());
		if (getFileName().length() > gettConf().getDendPoint())
			streamNo = getFileName().substring(gettConf().getDstartPoint(), getFileName().length());
		setStreamNo(streamNo);
	}
	
	public boolean checkISExist(Result res) throws Exception {
		// 判断流水号
		String _filename = HbaseUtil.getFromResult(res,TableUtil.configtabFamily,"fileName");
		//TableUtil.pm("TableFile-checkISExist", "compare "+_filename +"with" + getFileName());
		if (getFileName().equals(_filename)) {
        	rowKey = new String(res.getRow(),HbaseUtil.charSet);
        	return true;
        }
        else return false;
	}
    
	public boolean checkStreamNo() throws Exception {
		String streamKey =gettConf().tablename+getFinanceYear()+getFinanceMonth();
		//TableUtil.pm("TableFile-checkStreamNo", "doAction table:"+gettConf().tablename+" file:" +getFileName() );
		//IF Delete is needed compare streamNo 
		//System.out.println(tFile.gettConf().DBdelflag);
		if (gettConf().DBdelflag){
			Result result = HbaseUtil.QueryByConditionKey(TableUtil.streamchecktab, streamKey);
			if (result.isEmpty()) return true;
			else{
				int k = getModifieddate().
						compareTo(new String(result.getValue(TableUtil.streamcheckfamily.getBytes(),"modifieddate".getBytes() ),"UTF-8"));
				if (k>0) return true;
				else return false;
			}
		}else return true;
		
		
	}

	public String getStreamNo() {
		return streamNo;
	}

	public void setStreamNo(String streamNo) {
		this.streamNo = streamNo;
	}

	public String getFtpFileName() {
		return ftpFileName;
	}

	public void setFtpFileName(String ftpFileName) {
		this.ftpFileName = ftpFileName;
	}

	public void setFinanceDate() {
		this.financeDate = ftpFileName.substring(gettConf().getDstartPoint(), gettConf().getDendPoint()).replaceAll("\\-", "");// Date;
		this.financeYear = this.financeDate.substring(0, 4);
		this.financeMonth = this.financeDate.substring(4, 6);
	}
	public void setFinanceDate(String fString) {
		this.financeDate = fString;// Date;
		this.financeYear = fString.substring(0, 4);
		this.financeMonth = fString.substring(4, 6);
	}
	
	
	public TableAction gettAction() {
		return tAction;
	}

	public void settAction(TableAction tAction) {
		this.tAction = tAction;
	}

	public String getFinanceYear() {
		return financeYear;
	}

	public String getFinanceMonth() {
		return financeMonth;
	}

	public String getFinanceDate() {
		return financeDate;
	}

	public void setFileName() {
		setFileName(getFtpFileName().split("\\.")[0]);
	} 
	
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String filename) {
		this.fileName = filename;
	}

	public TableConf gettConf() {
		return tConf;
	}

	public void settConf(TableConf tConf) {
		this.tConf = tConf;
	}

	public Map<String, String> getFileCol() {
		return fileCol;
	}

	public void setFileCol(Map<String, String> fileCol) {
		this.fileCol = fileCol;
	}

	public String getRowkey() {
		return rowKey;
	}
	
}
